using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class PhieuThuTienDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SavePhieuThuTien(PhieuThuTien phieuthutien)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddPhieuThuTien", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddPhieuThuTien";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuthutien.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Prefix", DbType.String, ParameterDirection.Input, phieuthutien.Prefix);
            cmd.Parameters.Add(param);
            param = CreateParameter("@OrderNumber", DbType.String, ParameterDirection.Input, phieuthutien.OrderNumber);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, phieuthutien.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@History", DbType.String, ParameterDirection.Input, phieuthutien.History);
            cmd.Parameters.Add(param);
			param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, phieuthutien.NgayThang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdChiNhanhCongTy", DbType.Int32, ParameterDirection.Input, phieuthutien.IdChiNhanhCongTy);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdPhieuXuatHang", DbType.Int32, ParameterDirection.Input, phieuthutien.IdPhieuXuatHang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@LyDo", DbType.String, ParameterDirection.Input, phieuthutien.LyDo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DoiTuong", DbType.String, ParameterDirection.Input, phieuthutien.DoiTuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiLap", DbType.String, ParameterDirection.Input, phieuthutien.NguoiLap);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NguoiNopTien", DbType.String, ParameterDirection.Input, phieuthutien.NguoiNopTien);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoTien", DbType.Int64, ParameterDirection.Input, phieuthutien.SoTien);
			cmd.Parameters.Add(param);
            param = CreateParameter("@ChungTuKemTheo", DbType.String, ParameterDirection.Input, phieuthutien.ChungTuKemTheo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, phieuthutien.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all PhieuThuTien in DB with IsDelete = 0
		public ResultObj GetAllPhieuThuTien( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuThuTien> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuThuTien", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuThuTien>();
			PhieuThuTien item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuThuTien();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.IdPhieuXuatHang = Convert.ToInt32(reader["IdPhieuXuatHang"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.DoiTuong = Convert.ToString(reader["DoiTuong"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.NguoiNopTien = Convert.ToString(reader["NguoiNopTien"]);
			item.SoTien = Convert.ToInt64(reader["SoTien"]);
            item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all PhieuThuTien in DB with condition
		public ResultObj GetAllPhieuThuTienByID(PhieuThuTien phieuthutien)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuThuTien> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllPhieuThuTienByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuthutien.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuThuTien>();
			PhieuThuTien item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuThuTien();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.IdPhieuXuatHang = Convert.ToInt32(reader["IdPhieuXuatHang"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.DoiTuong = Convert.ToString(reader["DoiTuong"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.NguoiNopTien = Convert.ToString(reader["NguoiNopTien"]);
			item.SoTien = Convert.ToInt64(reader["SoTien"]);
            item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a PhieuThuTien out of DB with condition
		public ResultObj DeletePhieuThuTien(PhieuThuTien phieuthutien)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeletePhieuThuTien", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeletePhieuThuTien";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, phieuthutien.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}		
	}
}
